//
// Created by xili on 2024/9/9 13:32.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    vector <vector<int>> merge(vector <vector<int>> &intervals) {
        sort(intervals.begin(), intervals.end());
        vector <vector<int>> ans;
        for (auto &it: intervals) {
            int l = it[0], r = it[1];
            if (!ans.empty() && ans.back()[1] >= l) {
                ans.back()[1] = max(ans.back()[1], r);
            } else {
                ans.push_back({l, r});
            }
        }
        return ans;
    }
};